<template>
  <div>
    <el-container>
      <el-form
        label-position="right"
        ref="form"
        :model="form"
        label-width="100px"
      >
        <el-row v-for="(subModule, index) in subModuleList" :key="subModule">{{ subModule }}:
          <el-checkbox-group v-if="index == 0" v-model="form.defining_feature">
            <el-col :span="5" v-for="item in subModuleItemList[index]" :key="item">
              <el-checkbox :label="item">{{ item }}</el-checkbox>
            </el-col>
          </el-checkbox-group>
          <el-checkbox-group v-if="index == 1" v-model="form.relevant_factor">
            <el-col :span="5" v-for="item in subModuleItemList[index]" :key="item">
              <el-checkbox :label="item">{{ item }}</el-checkbox>
            </el-col>
          </el-checkbox-group>
          <el-checkbox-group v-if="index == 2" v-model="form.evidence">
            <el-col :span="5" v-for="item in subModuleItemList[index]" :key="item">
              <el-checkbox :label="item">{{ item }}</el-checkbox>
            </el-col>
          </el-checkbox-group>
        </el-row>
        <!-- 提交按钮 -->
        <el-row :gutter="10">
          <el-col :span="7">
            <div style="color: #ffffff">-</div>
          </el-col>
          <el-col :span="3">
            <el-form-item size="large">
              <el-button type="primary" @click.native.prevent="onSubmit"
                >提交</el-button
              >
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item size="large">
              <el-button type="danger" v-on:click="clearmsg">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-container>
  </div>
</template>

<script>
var BaaS = require("minapp-sdk");
let clientID = "9ed0a3da89683e42d793";
BaaS.init(clientID);
export default {
  name: "cognitiveNeurological",
  props: {
    module_cognitiveNeurological: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      subModuleList: [
        "定义性特征",
        "相关因素",
        "循证知识"
        ],
      subModuleListSet: new Set(),
      subModuleItemList: [
        [
        "记忆改变"],
        [
            "不合理的行为",
            "不正确的坚持指导",
            "缺乏信息",
            "认知功能改变",
            "正确的进行测试",
            "知识缺乏"
        ],
        [
            "家庭仪式破坏",
            "缺乏学习兴趣",
            "缺乏知识来源",
            "他人提供的错误信息"
        ]],
      submoduleItemListSet: new Set(),
      form: {
        defining_feature: [],
        relevant_factor: [],
        evidence: [],
      },
      formRules: {},
    };
  },
  methods: {
    getSubModuleItem(index1,index2) {
        let itemSet = new Set();
        // this.subModuleItemList[index2] = []; //将当前数组的元素设置为数组就是二维数组了
        let query = new BaaS.Query();
        let overall_eval = new BaaS.TableObject("overall_eval_item");
        query.compare("module", "=", this.module_cognitiveNeurological);
        query.compare("submodule", "=", this.subModuleList[index2]);
        overall_eval
          .setQuery(query)
          .select(["submodule", "item"])
          .limit(1000)
          .offset(index1)
          .find()
          .then((res) => {
            let datas = res.data.objects;
            datas.forEach((data) => {
              itemSet.add(data.item);
            });
            if (res.data.meta.next !== null) {
              this.getSubModuleItem(index1 + 1000,index2);
            } else {
              this.subModuleItemList.push(Array.from(itemSet));
              console.log(this.subModuleItemList[index2]);
            }
          });
    },
    getSubModule(index) {
      let query = new BaaS.Query();
      let overall_eval = new BaaS.TableObject("overall_eval_item");
      query.compare("module", "=", this.module_cognitiveNeurological);
      overall_eval
        .setQuery(query)
        .select(["module", "submodule"])
        .limit(1000)
        .offset(index)
        .find()
        .then((res) => {
          let datas = res.data.objects;
          datas.forEach((data) => {
            this.subModuleListSet.add(data.submodule);
          });
          if (res.data.meta.next !== null) {
            this.getSubModule(index + 1000);
          } else {
            this.subModuleList = Array.from(this.subModuleListSet);
            console.log(this.subModuleList);
            for (let i = 0; i < this.subModuleList.length; i++) {
                this.getSubModuleItem(0,i);
            }   
          }
        });
        
    },
    init() {
      this.getSubModule(0);
    },
  },
  mounted() {
    // this.init();
  },
};
</script>

<style>
</style>